package com.shupian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shupian.domain.ResponseResult;
import com.shupian.domain.dto.AddRoleDto;
import com.shupian.domain.dto.ChangeStatusDto;
import com.shupian.domain.dto.UpdateRoleDto;
import com.shupian.domain.entity.Role;
import com.shupian.domain.entity.RoleMenu;
import com.shupian.domain.vo.PageVo;
import com.shupian.domain.vo.RoleAVo;
import com.shupian.domain.vo.RoleVo;
import com.shupian.enums.AppHttpCodeEnum;
import com.shupian.mapper.RoleMapper;
import com.shupian.mapper.RoleMenuMapper;
import com.shupian.service.RoleMenuService;
import com.shupian.service.RoleService;
import com.shupian.utils.BeanCopyUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2022-08-28 11:19:06
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;


    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员 如果是返回集合中只需要有admin
        if(id==1L){
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户的权限信息

        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult getAllRole(Integer pageNum, Integer pageSize, String roleName, String status) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(roleName),Role::getRoleName,roleName);
        queryWrapper.eq(!StringUtils.isBlank(status),Role::getStatus,status);
        Page<Role> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Role> roles = page.getRecords();
        List<RoleVo> roleVos = BeanCopyUtils.copyBeanList(roles, RoleVo.class);
        PageVo pageVo = new PageVo(roleVos,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult changeStatus(ChangeStatusDto changeStatusDto) {
        Role role = getById(changeStatusDto.getRoleId());
        role.setStatus(changeStatusDto.getStatus());
        return updateById(role) ? ResponseResult.okResult() : ResponseResult.errorResult(500,"状态更新失败");
    }

    @Override
    public ResponseResult addRole(AddRoleDto addRoleDto) {
        List<String> menuIds = addRoleDto.getMenuIds();
        Role role = BeanCopyUtils.copyBean(addRoleDto, Role.class);
        if (save(role) && menuIds.size()>0) {
            Long id = role.getId();
            for (String menuId: menuIds) {
                RoleMenu roleMenu = new RoleMenu(id, Long.parseLong(menuId));
                roleMenuService.save(roleMenu);
            }
            return ResponseResult.okResult();
        }


        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult getRole(Long id) {
        Role role = getById(id);
        RoleAVo roleAVo = BeanCopyUtils.copyBean(role, RoleAVo.class);
        return ResponseResult.okResult(roleAVo);
    }

    @Override
    public ResponseResult updateRole(UpdateRoleDto updateRoleDto) {
        Role role = BeanCopyUtils.copyBean(updateRoleDto, Role.class);
        updateById(role);
        List<Long> menuIds = updateRoleDto.getMenuIds();
        Long id = updateRoleDto.getId();
        for (Long number: menuIds) {
            RoleMenu roleMenu = new RoleMenu(id,number);
            roleMenuService.save(roleMenu);
        }

        return ResponseResult.okResult();
    }
}

